package com.dony.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dony.common.utils.SecurityUtils;
import com.dony.shop.domain.ShopeeMerchant;
import com.dony.shop.mapper.ShopeeMerchantMapper;
import com.dony.shop.service.IShopeeMerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("shopeeMerchantService")
public class ShopeeMerchantServiceImpl extends ServiceImpl<ShopeeMerchantMapper, ShopeeMerchant> implements IShopeeMerchantService {

    @Override
    public ShopeeMerchant getByMerchantId(Long merchantId) {
        return getOne(new QueryWrapper<ShopeeMerchant>().eq("merchant_id", merchantId));
    }

    @Override
    public void updateToken(ShopeeMerchant merchant) {
        ShopeeMerchant dbMerchant = this.getByMerchantId(merchant.getMerchantId());
        if (dbMerchant != null) {
            dbMerchant.setLastRefreshToken(dbMerchant.getLastRefreshToken());
            updateById(dbMerchant);
        } else {
            log.error("错误：店铺不存在！店铺ID：{}", dbMerchant.getMerchantId());
        }

    }

    @Override
    public String selectLastRefreshToken(Long merchantId) {
        ShopeeMerchant merchant = this.getByMerchantId(merchantId);
        return merchant.getLastRefreshToken();
    }

    @Override
    public void setAuth(Long merchantId, boolean auth) {
        UpdateWrapper<ShopeeMerchant> wrapper = new UpdateWrapper<>();
        wrapper.set("auth", auth).eq("merchant_id", merchantId);
        this.update(wrapper);
    }

    @Override
    public List<ShopeeMerchant> listByParam(ShopeeMerchant merchant) {
        if (merchant == null) {
            return this.list();
        }
        QueryWrapper<ShopeeMerchant> queryWrapper = new QueryWrapper<>();
        if (merchant.getMerchantName() != null) {
            queryWrapper.like("merchant_name", merchant.getMerchantName());
        }
        queryWrapper.eq("tenant_id", SecurityUtils.getDeptId());
        return list(queryWrapper);
    }

    @Override
    public boolean updateRemark(ShopeeMerchant merchant) {
        if (merchant.getMerchantId() == null) {
            log.error("商户ID为空，无法修改信息！");
            return false;
        }
        UpdateWrapper<ShopeeMerchant> wrapper = new UpdateWrapper<>();
        wrapper.set("defind_name", merchant.getDefindName())
                .set("remark", merchant.getRemark())
                .eq("merchant_id", merchant.getMerchantId());
        return this.update(wrapper);
    }

    @Override
    public List<ShopeeMerchant> listByTenant() {
        return list(new QueryWrapper<ShopeeMerchant>().eq("tenant_id", SecurityUtils.getDeptId()));
    }

    @Override
    public Long getTenantByMerchant(Long merchantId) {
        ShopeeMerchant merchant = this.getById(merchantId);
        if (merchant != null) {
            return merchant.getTenantId();
        }
        return null;
    }


}
